/**
 * 
 */
package info.niwota.home.fm;

import instant.openvfs.home.Debug;
import jvs.vfs.FileSystem;
import x.java.io.File;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.util.Log;
import android.widget.Toast;

/**
 * @author qiangli
 * 
 */
public class ApplicationUtils {
	private static final long SHARE_FILE_LENGTH_LIMIT = 5L * 1024L * 1024L;
	private static final String LOG_TAG = "ApplicationUtils";

	/**
	 *
	 */
	private ApplicationUtils() {
	}

	public static void startViewActivity(Context ctx, File file) {
		try {
			Intent i = new Intent(Intent.ACTION_VIEW);
			String mimetype = file.getContentType();
			String u = file.toURI().toString();
			if (Debug.DEBUG) {
				Log.d(LOG_TAG, "startViewActivity mimetype: " + mimetype
						+ " URI: " + u);
			}
			Uri uri = Uri.parse(u);
			i.setDataAndType(uri, mimetype);
			//
			if (Debug.DEBUG) {
				Log.d(LOG_TAG, "startViewActivity mimetype: " + mimetype
						+ " uri: " + uri);
			}
			if (ctx.getPackageManager().resolveActivity(i,
					PackageManager.MATCH_DEFAULT_ONLY) != null) {
				if (Debug.DEBUG) {
					Log.d(LOG_TAG, "starting " + uri);
				}
				ctx.startActivity(Intent.createChooser(i, file.getName()));
				return;
			}
			// native
			java.io.File nf = FileSystem.getFileSystem().nativeFile(file);
			uri = Uri.parse(nf.toURI().toString());
			//
			i.setDataAndType(uri, mimetype);
			ctx.startActivity(Intent.createChooser(i, file.getName()));
		} catch (ActivityNotFoundException e) {
			Toast.makeText(ctx, e.getMessage(), Toast.LENGTH_LONG);
		} catch (Exception e) {
			Toast.makeText(ctx, e.getMessage(), Toast.LENGTH_LONG);
		}
	}

	public static void startSendActivity(Context ctx, File file) {
		try {
			long len = file.length();
			if (len > SHARE_FILE_LENGTH_LIMIT) {
				Toast.makeText(ctx, "File size too big", Toast.LENGTH_LONG)
						.show();
				return;
			}
			//
			String mimetype = file.getContentType();
			//
			Intent intent = new Intent();
			intent.setAction(Intent.ACTION_SEND);
			intent.setType(mimetype);
			intent.putExtra(Intent.EXTRA_SUBJECT, file.getName());
			intent.putExtra(Intent.EXTRA_TEXT, file.getPath());
			//
			boolean isLocal = file.isLocal();
			if (isLocal) {
				java.io.File nf = FileSystem.getFileSystem().nativeFile(file);
				Uri attach = Uri.parse(nf.toURI().toString());
				intent.putExtra(Intent.EXTRA_STREAM, attach);
			}
			String title = (isLocal ? "Send file" : "Send link");
			//
			ctx.startActivity(Intent.createChooser(intent, title));
		} catch (ActivityNotFoundException e) {
			Toast.makeText(ctx, e.getMessage(), Toast.LENGTH_LONG);
		} catch (Exception e) {
			Toast.makeText(ctx, e.getMessage(), Toast.LENGTH_LONG);
		}
	}

	public static void startShortcutActivity(Context ctx, File file) {
		Intent intent = new Intent(ctx, ShortcutActivity.class);
		intent.setData(Uri.parse(ShortcutActivity.SHORTCUT_SCHEME + "://"
				+ file.getPath()));
		ctx.startActivity(intent);
	}

	public static void startEditActivity(Context ctx, File file) {
		try {
			Intent i = new Intent(Intent.ACTION_EDIT);
			String mimetype = file.getContentType();
			Uri uri = Uri.parse(file.toURI().toString());
			i.setDataAndType(uri, mimetype);
			if (ctx.getPackageManager().resolveActivity(i,
					PackageManager.MATCH_DEFAULT_ONLY) != null) {
				if (Debug.DEBUG) {
					Log.d(LOG_TAG, "starting " + uri);
				}
				ctx.startActivity(Intent.createChooser(i, file.getName()));
				return;
			}
			// native
			java.io.File nf = FileSystem.getFileSystem().nativeFile(file);
			uri = Uri.parse(nf.toURI().toString());
			i.setDataAndType(uri, mimetype);
			ctx.startActivity(Intent.createChooser(i, file.getName()));
		} catch (ActivityNotFoundException e) {
			Toast.makeText(ctx, e.getMessage(), Toast.LENGTH_LONG);
		} catch (Exception e) {
			Toast.makeText(ctx, e.getMessage(), Toast.LENGTH_LONG);
		}
	}

}
